<template>
    <div class="content-box">
        <div class="search-container">
            <el-input style="width: 200px" placeholder="请输入进货单ID" suffix-icon="el-icon-search" v-model="id"></el-input>
            <el-button class="ml-5" type="primary" @click="handleSearch">搜索</el-button>
        </div>
        <!-- 表头 新增与批量删除-->
        <div class="search-container">
            <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
            <el-popconfirm class="ml-5" confirm-button-text='确定' cancel-button-text='取消' icon="el-icon-info"
                icon-color="red" title="您确定批量删除这些数据吗？" @confirm="deleteBatch">
                <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
            </el-popconfirm>

        </div>
        <!-- 表格 -->
        <div class="table-container">
            <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"
                @selection-change="handleSelectionChange">
                <el-table-column type="selection"></el-table-column>
                <el-table-column prop="id" label="进货单编号"></el-table-column>
                <el-table-column prop="stockTime" label="进货时间"></el-table-column>
                <el-table-column prop="staffId" label="采购人"></el-table-column>
                <el-table-column prop="stockMoney" label="进货总价"></el-table-column>
                <el-table-column label="操作" width="200" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" @click="handleEdit(scope.row)" icon="el-icon-edit" circle></el-button>
                        <el-popconfirm class="ml-5" confirm-button-text='确定' cancel-button-text='取消' icon="el-icon-info"
                            icon-color="red" title="您确定删除吗？" @confirm="del(scope.row.id)">
                            <el-button type="danger" slot="reference" icon="el-icon-delete" circle></el-button>
                        </el-popconfirm>
                        
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <!-- 分页 -->
        <div class="pagination-container">
            <el-pagination @size-change="changePageSize" @current-change="changePage" :current-page="searchForm.pageNumber"
                :page-size="searchForm.pageSize" layout="total, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>

        <!-- 新增,编辑框 -->
        <el-dialog v-model="form" title="进货详细信息" :visible.sync="dialogFormVisible" width="30%">
            <el-form label-width="80px" size="small">
                <el-form-item label="进货时间">
                    <el-input v-model="form.stockTime" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="负责人">
                    <el-input v-model="form.staffId" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="进货总价">
                    <el-input v-model="form.stockMoney" autocomplete="off"></el-input>
                </el-form-item>  
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="addOrUpdate">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
    
<script>
import { deletePurchaseOrder, getPurchaseOrderListData, postPurchaseOrder, putPurchaseOrder, deleteBatchPurchaseOrder } from "@/api/GoodsStock";
export default {
    name: "PurchaseOrder",
    data() {
        return {
            saveOrUpdate: true,//true代表新增，false代表更新
            total: 0, // 表单数据总数,
            tableData: [],
            ids: {},  // 批量删除id
            // 更新、新增表单
            form: {
                id: '',
                stockTime: '',
                staffId: '',
                stockMoney: '',
            },
            id: '',
            stockTime: '',
            // 分页对象
            searchForm: {
                pageNumber: 1, // 当前页数
                pageSize: 10 // 页面大小
            },
            dialogFormVisible: false,
            multipleSelection: [],
        }
    },
    methods: {
        // 初始化数据
        init() {
            this.getDataList();
        
        },
        // 分页 改变页码
        changePage(v) {
            this.searchForm.pageNumber = v;
            this.getDataList();
        },
       
        // 搜索
        handleSearch() {
            
            this.searchForm.id = this.id;
            this.searchForm.purchaseOrderStockTime = this.purchaseOrderStockTime;
            this.getDataList();
            this.searchForm.id = '';
            this.searchForm.purchaseOrderStockTime = '';
        },
        // 获取数据
        getDataList() {
            // this.loading = true;
            getPurchaseOrderListData(this.searchForm).then((res) => {
                if (res.data.success == true) {
                    this.tableData = res.data.result.records;
                    this.total = res.data.result.total;
                }
            });
        },

        // 新增显示模态款
        handleAdd() {
            this.dialogFormVisible = true;
            this.saveOrUpdate = true;
            this.form = {};
        
        },
        // 编辑显示模态款
        handleEdit(row) {
            this.form.id = row.id;
            this.form.stockTime = row.stockTime;
            this.form.staffId = row.staffId;
            this.form.stockMoney = row.stockMoney;
            this.dialogFormVisible = true;
            this.saveOrUpdate = false;
        },
       //多行选中
        handleSelectionChange(selection) {
            let arr = [];
            for (var i = 0; i < selection.length; i++) {
                arr[i] = selection[i].id;
            }
            this.ids.ids = arr + '';
            console.log(this.ids);
        },
        // 删除一条活动
        del(id) {
            deletePurchaseOrder(id).then((res) => {
                if (res.data.success == true) {
                    console.log("删除成功");
                }
                this.getDataList();
            })
        },
        // 新增与编辑
        addOrUpdate() {
            if (this.saveOrUpdate == true) {
                postPurchaseOrder(this.form).then((res) => {
    
                    if (res.data.success == true) {
                        this.dialogFormVisible = false;
                    }
                    this.getDataList();
                });
            }else{
                putPurchaseOrder(this.form).then((res) => {
                    if (res.data.success == true) {
                        this.dialogFormVisible = false;
                    }
                    this.getDataList();
                });
            }
        },
        deleteBatch() {
            deleteBatchPurchaseOrder(this.ids).then((res) => {
                if (res.data.success == true) {
                    console.log("删除成功");
                }
                this.getDataList();
            })
        }
    },
    mounted() {
        this.init();
    },
}
</script>
<style>
.headerBg {
    background: #eee !important;
}

.content-box {
    width: calc(100% - 10px);
    height: 100%;
    display: flex;
    flex-direction: column;
}

.table-container {
    flex: 1;
    overflow-y: auto;
}

.search-container {
    padding: 0, 10px;
}

.el-button.is-circle {
    margin-right: 10px;
}

.el-card__body,
.el-main {
    padding: 10px 20px 20px 20px;
}

.pagination-container {
    height: 30px;
    padding-top: 10px;
}
</style>